######################### Load Libraries #######################################

library(foreign)

library(car)

############################ Data Setup ########################################

setwd("~/_data/")

# read in cross-national election data
data <- read.csv("LossProbMaster_post1945_130707-MK.csv")

# save unmodified data as new object for backup
data.csv <- data

# eliminate empty cases
data <- data[data$case!="",]

# eliminate unnecessary columns
data <- data[,-which(colnames(data) %in% c("my.check.out","X","X.1","X.2"))]

# NOTE - Eastern Europe: Armenia (6 obs); Bulgaria" (8 obs); Croatia (7 obs); Czech Republic" (7 obs); Estonia (6 obs); Georgia (8 obs); Hungary (6 obs); Latvia (8 obs); Lithuania (7 obs); Moldova (7 obs); Poland (8 obs); Romania (7 obs); Russia (6 obs); Slovenia (7 obs); Switzerland (18 obs); Ukraine (6 obs)

# change "Czech" to "Czech Republic" for country variable
data[data$country=="Czech",2] <- "Czech Republic"

# make country factor variable 
data$country <- factor(data$country)

# make isocode factor variable
data$isocode <- factor(data$isocode)

# change name of year variable to elecyr
data$year <- data$elecyr

# change elecyr variable to two digits for years 19xx
data$elecyr <- ifelse(data$elecyr<2000,data$elecyr-1900,data$elecyr)

# generate variable that holds information on month and year of election date
data$Elecdate <- data$elecyr*100+data$elecmo

# generate new Country variable based on ISO code for merging with other data
data$Country <- Recode(data$isocode, "'AUL'=63; 'AUT'=42; 'BEL'=21; 'CAN'=62; 'DEU'=41; 'DNK'=13; 'ESP'=33; 'FIN'=14; 'FRA'=31; 'GBR'=51; 'GRC'=81; 'IRL'=53; 'ISL'=15; 'ITA'=32; 'JPN'=91; 'LUX'=23; 'NLD'=22; 'NOR'=12; 'NZL'=64; 'PRT'=35; 'SWE'=11; 'USA'=71; 'ALB'=101; 'ARM'=102; 'BGR'=103; 'CHE'=43; 'CZE'=104; 'EST'=105; 'GEO'=106; 'HRV'=107; 'HUN'=108; 'LTU'=109; 'LVA'=110; 'MDA'=111; 'POL'=112; 'ROU'=113; 'RUS'=114; 'SVN'=115; 'UKR'=116")

# convert Vanhannen competitive measures to numeric variable
data$van_comp <- as.numeric(levels(data$van_comp))[data$van_comp]/100

# remove elections that are before 1945 and the most recent ones for which we don't have data or where an electoral system change took place (FRA; ITA; JPN; NZL)
data <- data[-which(data$case %in% c("AUL1943_8","AUL2007_11","AUL2010_8","CAN2011_5","FRA1986_3","FRA2007_6","ITA1994_3","ITA1996_4","ITA2001_5","ITA2006_4","ITA2008_4","JPN1996_10","JPN2000_6","JPN2003_11","JPN2005_9","JPN2009_8","NZL1993_11","NZL1996_10","NZL1999_11","NZL2002_7","NZL2005_9","NZL2008_11","NZL2011_11","GBR2010_5","USA2008_11","USA2010_11")),]

# convert vote share variables to numeric format
data$VoteParty1_m1 <- as.numeric(levels(data$VoteParty1_m1))[data$VoteParty1_m1]
data$VoteParty2_m1 <- as.numeric(levels(data$VoteParty2_m1))[data$VoteParty2_m1]
data$VoteParty3_m1 <- as.numeric(levels(data$VoteParty3_m1))[data$VoteParty3_m1]
data$VoteParty4_m1 <- as.numeric(levels(data$VoteParty4_m1))[data$VoteParty4_m1]
data$VoteParty5_m1 <- as.numeric(levels(data$VoteParty5_m1))[data$VoteParty5_m1]
data$VoteParty6_m1 <- as.numeric(levels(data$VoteParty6_m1))[data$VoteParty6_m1]

# convert vote share variables to numeric format
data$VoteParty1_p1 <- as.numeric(levels(data$VoteParty1_p1))[data$VoteParty1_p1]
data$VoteParty2_p1 <- as.numeric(levels(data$VoteParty2_p1))[data$VoteParty2_p1]

# divide vote share variables by 100
data[,which(colnames(data) %in% c("VoteParty1","VoteParty1_m1","VoteParty1_p1","VoteParty2","VoteParty2_m1","VoteParty2_p1","VoteParty3","VoteParty3_m1","VoteParty4","VoteParty4_m1","VoteParty5","VoteParty5_m1","VoteParty6","VoteParty6_m1"))] <- data[,which(colnames(data) %in% c("VoteParty1","VoteParty1_m1","VoteParty1_p1","VoteParty2","VoteParty2_m1","VoteParty2_p1","VoteParty3","VoteParty3_m1","VoteParty4","VoteParty4_m1","VoteParty5","VoteParty5_m1","VoteParty6","VoteParty6_m1"))]/100

# generate vote share change variables for the top 2 vote-getting parties
data$DParty1 <- data$VoteParty1_p1-data$VoteParty1
data$DParty2 <- data$VoteParty2_p1-data$VoteParty2

# create new seat margin variable
data$seat_margin <- as.numeric(levels(data$SeatsParty1))[data$SeatsParty1] - as.numeric(levels(data$SeatsParty2))[data$SeatsParty2]

# calculate seat share gap variable
data$seatsharegap <- data$seat_margin/as.numeric(levels(data$seats_total))[data$seats_total]

# generate seats-votes elasticity variables for the top 2 parties and assign a value of 1 to PR systems
data$tau.Party1 <- ifelse(data$isocode %in% c("AUL","CAN","FRA","NZL","GBR","USA"),NA,1)
data$tau.Party2 <- ifelse(data$isocode %in% c("AUL","CAN","FRA","NZL","GBR","USA"),NA,1)

# enter seats-votes elasticity data for Australian elections (based on output generated from "sv_smd.R")
data[data$isocode=="AUL","tau.Party1"] <- c(2.74,3.06,2.72,3.42,2.89,2.95,2.83,2.95,2.51,3.36,3.16,2.90,3.13,3.02,3.07,3.24,3.51,3.69,3.98,3.58,3.30,3.45,3.73,3.67)
data[data$isocode=="AUL","tau.Party2"] <- c(2.08,2.98,2.83,2.99,2.96,2.79,2.74,3.15,2.82,3.07,2.73,2.80,3.41,3.10,3.27,2.92,3.37,3.70,4.05,3.65,3.10,3.44,3.64,3.46)

# enter seats-votes elasticity data for Canadian elections (based on output generated from "sv_smd.R")
data[data$isocode=="CAN","tau.Party1"] <- c(1.59,1.33,2.14,2.34,2.30,2.52,2.30,2.05,2.26,2.69,2.77,2.14,2.04,2.27,2.57,2.12,2.44,2.48,2.70,2.28,2.12)
data[data$isocode=="CAN","tau.Party2"] <- c(1.71,1.35,1.72,2.15,1.51,2.50,2.09,1.86,2.05,2.36,2.21,2.08,2.08,1.55,2.57,1.19,2.16,2.06,2.24,2.42,2.00)

# enter seats-votes elasticity data for French elections (based on output generated from "sv_smd.R")
data[data$isocode=="FRA","tau.Party1"] <- c(NA,NA,NA,3.14,4.28,3.98,3.90,2.29,3.65,3.51)
data[data$isocode=="FRA","tau.Party2"] <- c(NA,NA,NA,3.67,4.28,3.98,4.05,2.27,3.68,3.46)

# enter seats-votes elasticity data for New Zealand elections (based on output generated from "sv_smd.R")
data[data$isocode=="NZL","tau.Party1"] <- c(3.81,3.51,3.67,3.45,3.66,3.66,3.21,3.19,3.23,3.09,3.40,2.96,2.53,2.66,2.98,2.69)
data[data$isocode=="NZL","tau.Party2"] <- c(3.81,3.51,3.67,3.45,3.66,3.66,3.21,3.19,3.23,3.09,3.40,2.96,2.53,2.66,2.98,2.69)

# enter seats-votes elasticity data for UK elections (based on output generated from "sv_smd.R")
data[data$isocode=="GBR","tau.Party1"] <- c(2.06,2.61,2.93,2.88,2.76,2.65,2.44,2.92,2.58,2.70,3.31,3.10,2.91,3.05,2.16,2.26,2.45)
data[data$isocode=="GBR","tau.Party2"] <- c(2.05,2.92,2.90,2.86,2.65,3.10,2.76,2.46,3.33,3.48,2.28,2.35,2.12,2.17,2.70,2.90,2.74)

# enter seats-votes elasticity data for US elections (based on output generated from "sv_smd.R")
data[data$isocode=="USA","tau.Party1"] <- c(0.66,1.17,1.29,0.53,1.37,0.84,2.21,1.34,0.73,1.83,2.27,2.35,2.17,2.21,1.87,2.19,1.91,2.01,2.08,2.07,2.06,1.88,1.91,2.36,2.23,2.35,1.89,2.01,2.15,2.22,2.42)
data[data$isocode=="USA","tau.Party2"] <- c(0.68,1.07,1.20,0.44,1.45,0.89,2.15,1.36,0.73,1.90,2.26,2.24,2.14,2.22,1.88,2.16,1.96,1.96,2.14,2.08,2.02,1.82,1.97,2.38,2.26,2.45,1.91,2.13,2.16,2.37,2.34) 

# calculate seat swing variable for PR countries
data$s.pr <- ifelse(data$isocode %in% c("AUL","CAN","FRA","NZL","GBR","USA"),NA,data[,"DParty1"]*data[,"tau.Party1"] - data[,"DParty2"]*data[,"tau.Party2"])

# generate an empty vector for the LPR measure
data$lossprob_statictau <- rep(NA,nrow(data))

####################################
#### Australia: LPR calculation ####
####################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="AUL",]))

for (i in 7:length(s)) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="AUL",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="AUL","DParty1"][j]*data[data$isocode=="AUL","tau.Party1"][i] - data[data$isocode=="AUL","DParty2"][j]*data[data$isocode=="AUL","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:length(s)) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="AUL","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="AUL","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.aul <- s

# remove seat swing variable with old name 
rm(s)


#################################
#### Canada: LPR calculation ####
#################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="CAN",]))

for (i in 7:length(s)) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="CAN",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="CAN","DParty1"][j]*data[data$isocode=="CAN","tau.Party1"][i] - data[data$isocode=="CAN","DParty2"][j]*data[data$isocode=="CAN","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:length(s)) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="CAN","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="CAN","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.can <- s

# remove seat swing variable with old name
rm(s)


#################################
#### France: LPR calculation ####
#################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="FRA",]))

for (i in 7:length(s)) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="FRA",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="FRA","DParty1"][j]*data[data$isocode=="FRA","tau.Party1"][i] - data[data$isocode=="FRA","DParty2"][j]*data[data$isocode=="FRA","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:length(s)) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="FRA","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="FRA","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.fra <- s

# remove seat swing variable with old name
rm(s)


######################################
#### New Zealand: LPR calculation ####
######################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="NZL",]))

for (i in c(7:length(s))) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="NZL",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="NZL","DParty1"][j]*data[data$isocode=="NZL","tau.Party1"][i] - data[data$isocode=="NZL","DParty2"][j]*data[data$isocode=="NZL","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in c(7:length(s))) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="NZL","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="NZL","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.nzl <- s

# remove seat swing variable with old name
rm(s)


#########################################
#### United Kingdom: LPR calculation ####
#########################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="GBR",]))

for (i in 7:length(s)) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="GBR",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="GBR","DParty1"][j]*data[data$isocode=="GBR","tau.Party1"][i] - data[data$isocode=="GBR","DParty2"][j]*data[data$isocode=="GBR","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:length(s)) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="GBR","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="GBR","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.gbr <- s

# remove seat swing variable with old name
rm(s)


########################################
#### United States: LPR calculation ####
########################################

# calculate seat swing
s <- vector("list",nrow(data[data$isocode=="USA",]))

for (i in 7:length(s)) {

	s[[i]] <- rep(NA,nrow(data[data$isocode=="USA",]))
	
		for (j in 1:(i-1)) {
	
			s[[i]][j] <- data[data$isocode=="USA","DParty1"][j]*data[data$isocode=="USA","tau.Party1"][i] - data[data$isocode=="USA","DParty2"][j]*data[data$isocode=="USA","tau.Party2"][i]
	
		}

}


# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:length(s)) {
	
	z.smd <- density(s[[i]][is.na(s[[i]])==F])
	f.smd <- approxfun(z.smd$x, z.smd$y, yleft = 0, yright = 0)
	data[data$isocode=="USA","lossprob_statictau"][i] <- integrate(f.smd, -Inf, -data[data$isocode=="USA","seatsharegap"][i])$value

}

# generate renamed copy of seat swing variable
s.usa <- s

# remove seat swing variable with old name
rm(s)


##################################
#### Austria: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="AUT",])) {
	
	z.pr <- density(data[data$isocode=="AUT","s.pr"][-c(i:nrow(data[data$isocode=="AUT",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="AUT","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="AUT","seatsharegap"][i])$value

}


##################################
#### Belgium: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="BEL",])) {
	
	z.pr <- density(data[data$isocode=="BEL","s.pr"][-c(i:nrow(data[data$isocode=="BEL",]),which(is.na(data[data$isocode=="BEL","s.pr"][-c(i:nrow(data[data$isocode=="BEL",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="BEL","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="BEL","seatsharegap"][i])$value

}


##################################
#### Germany: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="DEU",])) {
	
	z.pr <- density(data[data$isocode=="DEU","s.pr"][-c(i:nrow(data[data$isocode=="DEU",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="DEU","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="DEU","seatsharegap"][i])$value

}

##################################
#### Denmark: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in c(7:11,13:nrow(data[data$isocode=="DNK",]))) {
	
	z.pr <- density(data[data$isocode=="DNK","s.pr"][-c(i:nrow(data[data$isocode=="DNK",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="DNK","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="DNK","seatsharegap"][i])$value

}


################################
#### Spain: LPR calculation ####
################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="ESP",])) {
	
	z.pr <- density(data[data$isocode=="ESP","s.pr"][-c(i:nrow(data[data$isocode=="ESP",]),which(is.na(data[data$isocode=="ESP","s.pr"][-c(i:nrow(data[data$isocode=="ESP",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="ESP","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="ESP","seatsharegap"][i])$value

}


##################################
#### Finland: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="FIN",])) {
	
	z.pr <- density(data[data$isocode=="FIN","s.pr"][-c(i:nrow(data[data$isocode=="FIN",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="FIN","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="FIN","seatsharegap"][i])$value

}


#################################
#### Greece: LPR calculation ####
#################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="GRC",])) {
	
	z.pr <- density(data[data$isocode=="GRC","s.pr"][-c(i:nrow(data[data$isocode=="GRC",]),which(is.na(data[data$isocode=="GRC","s.pr"][-c(i:nrow(data[data$isocode=="GRC",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="GRC","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="GRC","seatsharegap"][i])$value

}


##################################
#### Ireland: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="IRL",])) {
	
	z.pr <- density(data[data$isocode=="IRL","s.pr"][-c(i:nrow(data[data$isocode=="IRL",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="IRL","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="IRL","seatsharegap"][i])$value

}


##################################
#### Iceland: LPR calculation ####
##################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in c(7:14,16:nrow(data[data$isocode=="ISL",]))) {
	
	z.pr <- density(data[data$isocode=="ISL","s.pr"][-c(i:nrow(data[data$isocode=="ISL",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="ISL","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="ISL","seatsharegap"][i])$value

}


################################
#### Italy: LPR calculation ####
################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="ITA",])) {
	
	z.pr <- density(data[data$isocode=="ITA","s.pr"][-c(i:nrow(data[data$isocode=="ITA",]),which(is.na(data[data$isocode=="ITA","s.pr"][-c(i:nrow(data[data$isocode=="ITA",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="ITA","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="ITA","seatsharegap"][i])$value

}


################################
#### Japan: LPR calculation ####
################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:(nrow(data[data$isocode=="JPN",]))) {
	
	z.pr <- density(data[data$isocode=="JPN","s.pr"][-c(i:nrow(data[data$isocode=="JPN",]),which(is.na(data[data$isocode=="JPN","s.pr"][-c(i:nrow(data[data$isocode=="JPN",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="JPN","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="JPN","seatsharegap"][i])$value

}


#####################################
#### Luxembourg: LPR calculation ####
#####################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="LUX",])) {
	
	z.pr <- density(data[data$isocode=="LUX","s.pr"][-c(i:nrow(data[data$isocode=="LUX",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="LUX","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="LUX","seatsharegap"][i])$value

}


######################################
#### Netherlands: LPR calculation ####
######################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="NLD",])) {
	
	z.pr <- density(data[data$isocode=="NLD","s.pr"][-c(i:nrow(data[data$isocode=="NLD",]),which(is.na(data[data$isocode=="NLD","s.pr"][-c(i:nrow(data[data$isocode=="NLD",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="NLD","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="NLD","seatsharegap"][i])$value

}


#################################
#### Norway: LPR calculation ####
#################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in c(7:12,14:nrow(data[data$isocode=="NOR",]))) {
	
	z.pr <- density(data[data$isocode=="NOR","s.pr"][-c(i:nrow(data[data$isocode=="NOR",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="NOR","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="NOR","seatsharegap"][i])$value

}


###################################
#### Portugal: LPR calculation ####
###################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 8:nrow(data[data$isocode=="PRT",])) {
	
	z.pr <- density(data[data$isocode=="PRT","s.pr"][-c(i:nrow(data[data$isocode=="PRT",]),which(is.na(data[data$isocode=="PRT","s.pr"][-c(i:nrow(data[data$isocode=="PRT",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="PRT","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="PRT","seatsharegap"][i])$value

}

#################################
#### Sweden: LPR calculation ####
#################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in c(7:8,10:nrow(data[data$isocode=="SWE",]))) {
	
	z.pr <- density(data[data$isocode=="SWE","s.pr"][-c(i:nrow(data[data$isocode=="SWE",]))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="SWE","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="SWE","seatsharegap"][i])$value

}


######################################
#### Switzerland: LPR calculation ####
######################################

# calculate LPR (drop first six elections and those for which integral cannot be calculated)
for (i in 7:nrow(data[data$isocode=="CHE",])) {
	
	z.pr <- density(data[data$isocode=="CHE","s.pr"][-c(i:nrow(data[data$isocode=="CHE",]),which(is.na(data[data$isocode=="CHE","s.pr"][-c(i:nrow(data[data$isocode=="CHE",]))])))])
	f.pr <- approxfun(z.pr$x, z.pr$y, yleft = 0, yright = 0)
	data[data$isocode=="CHE","lossprob_statictau"][i] <- integrate(f.pr, -Inf, -data[data$isocode=="CHE","seatsharegap"][i])$value

}

############################## Save Data #######################################

save.image(file = "lprdata.RData")